package com.example.testone

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.d("GaoChang", "onCreate: ")

//        var bookModel = BookModel()
//        bookModel.fetchTag(OnSuccess = {
//            Log.d("GaoChang", "OnSuccess: $it")
//        }, OnFailure = {
//            Log.d("GaoChang", "OnFailure: $it")
//        }, false)

        //Log.d("GaoChang", "onCreate: $str")
        val items = listOf(1,2,3,4,5)

        val result = items.fold(0, { acc: Int, nextElement: Int ->
            val result = acc + nextElement
            result
        })
        println(result)

        val result2 = items.fold("Elements:",{acc: String, nextElement: Int ->
            "$acc $nextElement"
        })
        println(result2)

        val result3 = items.fold(1, Int::times) //xiangcheng
        println(result3)

        var i = {a:Int,b:Int -> "${a + b} hh"}
        println(i(2,3))

        val repeatFun: String.(Int) -> String = { it -> this.repeat(it) }
        val repeatFun2 = {str:String, repeatCount:Int->str.repeat(repeatCount)}
        println(repeatFun("2",3))
        println(repeatFun2("3",3))
        val twoParamenters:(String, Int) -> String = repeatFun //函数类型

        fun runTransformation(f:(String,Int)->String):String{
            return f("Hello", 3)
        }
        println(runTransformation(repeatFun))

        val stringPlus:(String, Int) -> String  = String::plus
        println(stringPlus("11",1222))

        val intPlus:Int.(Int) -> Int = Int::plus

        println(2.intPlus(3))


        val sum = {x: Int, y: Int -> x + y}

        val sum2:Int.(Int)->Int = {
                other -> plus(other)}

        fun num1AndNum2(num1: Int, num2: Int, operation: (Int, Int) -> Int): Int {
            return operation(num1, num2)
        }

        fun addNum(num1: Int, num2: Int):Int{
            return num1 + num2
        }


        fun minNum(num1: Int, num2: Int):Int{
            return num1 - num2
        }
        println(num1AndNum2(1,2,::addNum))



        fun StringBuilder.build(block:StringBuilder.()->Unit):StringBuilder{
            block()
            return this
        }

        val list = listOf("Apple","Banana","Orange","Pear","Grape")
        val result12 = StringBuilder().build{
            append("Start eating fruit.\n")
            for(fruit in list){
                append(fruit).append("\n")
            }
            append("Ate all fruits")
        }
        println(result12.toString())



    }



    fun <T, R> Collection<T>.fold(initial: R, combine: (acc: R, nextElement: T) -> R): R {
        var accumulator: R = initial
        for (element: T in this) {
            println("gaochang$element")
            accumulator = combine(accumulator, element)
        }
        return accumulator
    }
}